#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>
#include <QImage>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    paintImage();
    setPixel();
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 在图像上绘图
void MainWindow::paintImage()
{
    QPainter painter;
    QImage destImage;
    destImage.load(":/images/default.png");
    QImage sourceImage;
    sourceImage.load(":/images/luban.jpg");
    painter.begin(&destImage);
    QRect rect = sourceImage.rect();
    rect.moveCenter(destImage.rect().center());
    painter.drawImage(rect, sourceImage);
    painter.end();
    destImage.save("./default_luban.png");
    destImage.mirrored(true, false).save("mirrored.png");
}

void MainWindow::setPixel()
{
    QImage image;
    image.load(":/images/default.png");
    for(int x = 145; x < 145 + 10; x++){
        for(int y = 0; y < 300; y++){
            image.setPixelColor(x, y, Qt::red);
        }
    }
    image.save("defalut_pixel.png");
}

// 在窗体上绘图
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter;
    painter.begin(this);

    QImage image(":/images/default.png");
    QRect rect;
    rect = image.rect();
    painter.drawImage(rect, image);

    rect = QRect(50, 50, 100, 100);
    painter.drawImage(rect, image);

    rect.moveCenter(this->rect().center());
    painter.drawImage(rect, image);

    painter.end();
}
